package controller;


import com.google.gson.Gson;
import dao.UserDaoImpl;
import domain.Equipment;
import domain.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 简介说明: 用户servlet

 * @author: Half_Q_0115
 * @date: 2024/09/16 21:15:10
 * @version: 1.0
 */

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UserDaoImpl userDao = new UserDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        if("getAllUser".equals(method)){
            this.getAllUser(req,resp);
        }else if("searchUser".equals(method)){
            this.searchUser(req,resp);
        }else if("getUserById".equals(method)){
            this.getUserById(req, resp);
        }else if ("modifyUser".equals(method)) {
            this.modifyUser(req, resp);
        } else if ("addUser".equals(method)) {
            this.addUser(req, resp);
        } else if ("deleteUserById".equals(method)) {
            this.deleteUser(req, resp);
        } else{
            System.out.println("请求地址错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    private void getAllUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<User> list = userDao.findAllUser();
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void searchUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String collegeId = req.getParameter("college_id");
        String identity = req.getParameter("identity");
        String phoneNumber = req.getParameter("phone_number");

        List<User> list = userDao.searchUsers(id, name, gender, collegeId, identity, phoneNumber);
        HashMap<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);

        Gson gson = new Gson();
        String json = gson.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();
    }
    private void getUserById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        User user = userDao.findUserById(id);
        HashMap<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", user);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();
    }
    private void modifyUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String data = req.getParameter("data");
        Gson gson = new Gson();
        User user = gson.fromJson(data, User.class);

        int ret = userDao.modifyUser(user);
        PrintWriter writer = resp.getWriter();
        if (ret > 0) {
            writer.print(1);
        } else {
            writer.print(0);
        }
        writer.close();
    }

    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int result = userDao.deleteUserById(id);
        PrintWriter writer = resp.getWriter();
        if (result > 0) {
            writer.print(1);
        }else {
            writer.print(0);
        }
        writer.close();
    }

    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String data = req.getParameter("data");
        Gson gson = new Gson();
        User user = gson.fromJson(data, User.class);
        int ret = userDao.addUser(user);
        PrintWriter writer = resp.getWriter();
        if(ret>0){
            writer.print(1);
        }else {
            writer.print(0);
        }
        writer.close();
    }


}
